package com.purong.purong.activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.adapter.LoanQuestionAdapter;
import com.purong.purong.bean.QuestionBean;
import com.purong.purong.bean.BankLoanQuestListBean;
import com.purong.purong.widget.LoadingView;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrCustomHandler;
import in.srain.cube.views.ptr.PtrDefaultCustomHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrPuRongLayout;

/**
 * Created by wenjiezeng on 2017/11/6.
 */
public class LoanQuestionActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private LoanQuestionAdapter mAdapter;
    private List<QuestionBean> questList=new ArrayList<>();
    private PtrPuRongLayout mPtrFrame;
    private LoadingView loadView;
    private String agencyId;
    private int page=1;
    @Override
    public void initView() {
        loadView=(LoadingView)findViewById(R.id.loading_view);
        loadView.setNoResultToast("暂无问题",null);
        loadView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getQuestion(true);
            }

            @Override
            public void onRefresh() {

            }
        });
        mPtrFrame = (PtrPuRongLayout) findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setPtrHandler(new PtrCustomHandler() {
            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {

                if (questList.size()==0){
                    return false;
                }else {
                    return PtrDefaultCustomHandler.checkContentCanBePulledUp(frame, content, footer);
                }

            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getQuestion(false);

            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getQuestion(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return false;
            }
        });

        recyclerView=(RecyclerView)findViewById(R.id.question_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
        mAdapter=new LoanQuestionAdapter(this);
        mAdapter.setData(questList);
        mAdapter.setOnLoanQuestionListener(new LoanQuestionAdapter.OnLoanQuestionListener() {
            @Override
            public void onQuestionClick(QuestionBean bean) {
                Intent intent=new Intent(LoanQuestionActivity.this,HelpQuestDetActivity.class);
                intent.putExtra("QUESTION",bean);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void initBundle() {
        agencyId=getIntent().getStringExtra("AGENCY_ID");
    }

    @Override
    public void initData() {
        getQuestion(true);
    }
    private void getQuestion(boolean clear){
        if (agencyId==null)
            return;
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("loan_agency_id",agencyId);
        if (clear){
            page=1;
            questList.clear();
            mAdapter.notifyDataSetChanged();
            loadView.loading();
        }
        params.put("page",page+"");
        if (!request(Contant.LOAN_QUESTION_LIST, params, true)) {
            if (clear)
                loadView.fail();
            if (!hasDestroy())
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (mPtrFrame.isRefreshing()) {
                            mPtrFrame.refreshComplete();
                        }
                    }
                }, 100);
        }
    }
    @Override
    public int getRootViewId() {
        return R.layout.activity_loan_question;
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.LOAN_QUESTION_LIST)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try{
                    BankLoanQuestListBean bean= JSON.parseObject(responseInfo.content,BankLoanQuestListBean.class);
                    if (bean!=null&&bean.getLoan_question_list()!=null){
                        if (bean.getLoan_question_list().size()>0)
                            page++;
                        questList.addAll(bean.getLoan_question_list());
                        mAdapter.notifyDataSetChanged();
                    }
                    if (questList.size()==0){
                        loadView.noResult();
                    }else {
                        loadView.finish();
                    }
                }catch (Exception e){
                    if (page==1)
                        loadView.fail();
                }
            }else {
                if (page==1)
                    loadView.fail();
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.ask_question:
                Intent intent=new Intent(this,LoanAskQuestActivity.class);
                intent.putExtra("AGENCY_ID",agencyId);
                startActivity(intent);
                break;
            case R.id.close:
                finish();
                break;
        }
    }
}
